package com.example.administrator.szb.activity.supei.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.supei.model.SuPeiModelImpl;
import com.example.administrator.szb.activity.supei.view.SuPeiView;
import com.example.administrator.szb.bean.UserInfo2;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.SPUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by stone on 2018/1/24.
 */

public class SuPeiPresenter extends BasePresenter<SuPeiView> implements OnResultListener{
    private SuPeiView suPeiView;
    private SuPeiModelImpl model;
    public SuPeiPresenter(SuPeiView suPeiView) {
        this.suPeiView = suPeiView;
        model = new SuPeiModelImpl();
    }

    public void send(){
        if (suPeiView.canSubmit()){
            model.doFabu(activity,suPeiView.getSubmitInfo(),this);
        }

    }


    @Override
    public void onSuccess(int requestCode, String str) {
        suPeiView.onSuccesss(str);
    }

    @Override
    public void onFailure(int requestCode, String str) {
        suPeiView.onFails(str);
    }

    public void requestUserInfo(){
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("id", SPUtils.getUserId());
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_CENTER, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                SPUtils.setDatas(SPUtils.USERS_INTO,str);
                UserInfo2 userInfo2 = JSON.parseObject(str,new TypeReference<UserInfo2>(){});
                String namelist = userInfo2.getReal_name();
                if (TextUtils.isEmpty(namelist)){
                    namelist = "[]";
                }
                List<String> real_name = JSON.parseArray(namelist,String.class);
                if (real_name.size()>0){
                    if (userInfo2.getGenre()==1){
                        suPeiView.showName(real_name.get(0));
                    }else {
                        suPeiView.showName(real_name.get(0));
                        if (real_name.size()>1){
                            suPeiView.showName(real_name.get(0)+real_name.get(1));
                        }

                    }
                }

            }

            @Override
            public void onFailure(int requestCode, String str) {
            }
        });
    }


}
